//package com.weixin.engine.filter;
//
//import com.alibaba.fastjson.JSON;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.util.CollectionUtils;
//
//import javax.servlet.Filter;
//import javax.servlet.FilterChain;
//import javax.servlet.FilterConfig;
//import javax.servlet.ServletException;
//import javax.servlet.ServletRequest;
//import javax.servlet.ServletResponse;
//import javax.servlet.annotation.WebFilter;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.io.BufferedReader;
//import java.io.IOException;
//import java.util.HashMap;
//import java.util.Set;
//import java.util.stream.Collectors;
//
///**
// * 参数过滤器
// *
// * @author wangchunhui
// * @date 2022/4/28 21:14
// */
//@Configuration
//@WebFilter(filterName = "idFilter", urlPatterns = {"/*"})
//public class ParameterFilter implements Filter {
//
//    @Override
//    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
//            ServletException {
//        try {
//            HttpServletRequest httpServletRequest = (HttpServletRequest) request;
//            HttpServletResponse httpServletResponse = (HttpServletResponse) response;
//            String path = httpServletRequest.getRequestURI();
//            Set<String> notAllowedPathsSet = GuaranteeGlobalConstant.NOT_ALLOWED_PATHS_SET.stream().filter(path::contains).collect(Collectors.toSet());
//            if (!CollectionUtils.isEmpty(notAllowedPathsSet)){
//                //证明是不需要加解密的路径，放行
//                chain.doFilter(request, response);
//                return;
//            }
//            //1、重写 RequestWrapper，重写获取流的方法
//            RequestWrapper requestWrapper = new RequestWrapper((HttpServletRequest) request);
//            //2、读取输入流，将所需信息写入
//            //2.1、json形式参数填充
//            StringBuffer buffer = new StringBuffer();
//            String line = null;
//            BufferedReader reader = null;
//            reader = requestWrapper.getReader();
//            while ((line = reader.readLine()) != null) {
//                buffer.append(line);
//            }
//            Object object;
//            try {
//                object = JSON.parseObject(buffer.toString());
//            } catch (Exception e) {
//                object = JSON.parseArray(buffer.toString());
//            }
//            if (object != null) {
//                DesensitizeUtil.analyzeJson(object, GuaranteeGlobalConstant.DECRYPT_FLAG);
//                requestWrapper.setBody(object.toString().getBytes());
//            }
//            //2.2、普通形式参数填充
//            //因为@PathVariable注解是拼接在url上的，Filter无法拿到它的属性名和值，所以我们在此通过判断是否解密成功来决定url路径
//            //前提是我们默认最后一个字段来处理
////            judgeForward(httpServletRequest, httpServletResponse);
//
//            String url = httpServletRequest.getRequestURI();
//            String lastStrInUrl = url.substring(url.lastIndexOf("/") + 1, url.length());
//            String preStrInUrl = url.substring(0, url.lastIndexOf("/") + 1);
//
//            String decryptLastStrInUrl = null;
//            try {
//                decryptLastStrInUrl = DesensitizeUtil.decrypt(lastStrInUrl);
//            } catch (Exception e) {
//                //证明不是加密字符串,do nothing
//            }
//            if (decryptLastStrInUrl != null) {
//                //证明解密成功了，转发！
//                httpServletResponse.sendRedirect(preStrInUrl + decryptLastStrInUrl);
//                return;
//            }
//
//            HashMap parameterMap = new HashMap(requestWrapper.getParameterMap());
//            Object idObj = parameterMap.get(GuaranteeGlobalConstant.DECRYPT_FIELD);
//            if (idObj != null) {
//                String[] idObjArray = (String[]) idObj;
//                parameterMap.put(GuaranteeGlobalConstant.DECRYPT_FIELD, new String[]{DesensitizeUtil.decrypt(idObjArray[0])});
//            }
//            ParameterRequestWrapper newRequest = new ParameterRequestWrapper(requestWrapper, parameterMap);
//            //3、过滤跳转
//            chain.doFilter(newRequest, response);
//        } catch (Exception e) {
//            chain.doFilter(request, response);
//        }
//    }
//
//    private void judgeForward(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException {
//        String url = httpServletRequest.getRequestURI();
//        String lastStrInUrl = url.substring(url.lastIndexOf("/") + 1, url.length());
//        String preStrInUrl = url.substring(0, url.lastIndexOf("/") + 1);
//
//        String decryptLastStrInUrl = null;
//        try {
//            decryptLastStrInUrl = DesensitizeUtil.decrypt(lastStrInUrl);
//        } catch (Exception e) {
//            //证明不是加密字符串,do nothing
//        }
//        if (decryptLastStrInUrl != null) {
//            //证明解密成功了，转发！
//            httpServletRequest.getRequestDispatcher(preStrInUrl + decryptLastStrInUrl).forward(httpServletRequest,
//                    httpServletResponse);
//        }
//    }
//
//    @Override
//    public void destroy() {
//
//    }
//
//    @Override
//    public void init(FilterConfig config) {
//
//    }
//}
